package com.xxd.rpt3.config;

import java.util.List;

import com.xxd.utils.EHCacheUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.xxd.rpt3.Entity.Area;
import com.xxd.rpt3.Entity.Department;
import com.xxd.rpt3.Entity.Menu;
import com.xxd.rpt3.Entity.Permission;
import com.xxd.rpt3.Entity.Role;
import com.xxd.rpt3.Entity.RoleType;
import com.xxd.rpt3.Entity.User;
import com.xxd.rpt3.repository.AreaRepository;
import com.xxd.rpt3.repository.DepartmentRepository;
import com.xxd.rpt3.repository.MenuRepository;
import com.xxd.rpt3.repository.PermissionRepository;
import com.xxd.rpt3.repository.RoleRepository;
import com.xxd.rpt3.repository.RoleTypeRepository;
import com.xxd.rpt3.repository.UserRepository;

/**
 * @Author XuXd
 * @Description: 初始化菜单、用户等数据
 * @CreateDate: 11:49 2019-09-11
 * @Modified by: *
 */
@Configuration
class InitData {

	@Autowired
	UserRepository userRepository;
	@Autowired
	DepartmentRepository departmentRepository;
	@Autowired
	AreaRepository areaRepository;
	@Autowired
	RoleRepository roleRepository;
	@Autowired
	MenuRepository menuRepository;
	@Autowired
	RoleTypeRepository roleTypeRepository;
	@Autowired
	PermissionRepository permissionRepository;

	@Bean
	public void initDbData() {
		EHCacheUtils.clearTokenCache();
		long flag = roleTypeRepository.count();
		if (flag <= 0) {
			initPermission();
			initMenu();
			initRoleType();
			initRole();
			initDepartment();
			initArea();
			initAdmin();
		}
	}

	public void initDepartment() {
		Department depa = new Department();
		depa.setName("ALL");
		depa.setCode("ALL");
		departmentRepository.save(depa);
	}

	public void initArea() {
		Area area = new Area();
		area.setName("ALL");
		area.setCode("ALL");
		areaRepository.save(area);
	}

	public void initAdmin() {
		List<Role> roleList = roleRepository.findAll();
		Department department = departmentRepository.findAll().get(0);
		Area area = areaRepository.findAll().get(0);
		User user = new User();
		user.setName("管理员");
		user.setCode("ADMIN");
		user.setPassword("1");
		user.setDepartment(department);
		user.setArea(area);
		user.setRoleList(roleList);
		userRepository.save(user);
	}

	public void initRoleType() {
		RoleType area = new RoleType();
		area.setName("功能角色");
		area.setCode("FUNC_ROLE");
		roleTypeRepository.save(area);
		area.setName("数据角色");
		area.setCode("DATA_ROLE");
		roleTypeRepository.save(area);
	}

	public void initRole() {
		List<Menu> menuList = menuRepository.findAll();
		RoleType roleType = roleTypeRepository.findAll().get(0);
		Role role = new Role();
		role.setCode("ADMIN_ROLE");
		role.setName("管理员");
		role.setRoleType(roleType);
		role.setMenuList(menuList);
		roleRepository.save(role);
	}

	public void initPermission() {
		Permission area = new Permission();
		area.setName("查询");
		area.setCode("query");
		permissionRepository.save(area);
	}

	public void initMenu() {
		List<Permission> permissionList = permissionRepository.findAll();
		Menu area = new Menu();
		area.setName("ROOT");
		area.setCode("ROOT");
		area.setPermissionList(permissionList);
		menuRepository.save(area);
	}
}
